{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Access intermediate steps\n",
    "In order to get more visibility into what an agent is doing, we can also return intermediate steps. This comes in the form of an extra key in the return value, which is a list of (action, observation) tuples.\n",
    "\n",
    "为了更清楚地了解座席正在做什么，我们还可以返回中间步骤。这在返回值中以额外键的形式出现，该键是（操作、观察）元组的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `wikipedia` with `Leonardo DiCaprio`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mPage: Leonardo DiCaprio\n",
      "Summary: Leonardo Wilhelm DiCaprio (; Italian: [diˈkaːprjo]; born November 1\u001b[0m\u001b[32;1m\u001b[1;3mLeonardo DiCaprio's middle name is Wilhelm.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': \"What is Leo DiCaprio's middle name?\",\n",
       " 'output': \"Leonardo DiCaprio's middle name is Wilhelm.\",\n",
       " 'intermediate_steps': [(AgentActionMessageLog(tool='wikipedia', tool_input='Leonardo DiCaprio', log='\\nInvoking: `wikipedia` with `Leonardo DiCaprio`\\n\\n\\n', message_log=[AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '{\"__arg1\":\"Leonardo DiCaprio\"}', 'name': 'wikipedia'}}, response_metadata={'finish_reason': 'function_call'}, id='run-6b7bb264-af90-4f2b-9eaf-0eecf0021621')]),\n",
       "   'Page: Leonardo DiCaprio\\nSummary: Leonardo Wilhelm DiCaprio (; Italian: [diˈkaːprjo]; born November 1')]}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain import hub\n",
    "from langchain.agents import AgentExecutor, create_openai_functions_agent\n",
    "from langchain_community.tools import WikipediaQueryRun\n",
    "from langchain_community.utilities import WikipediaAPIWrapper\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)\n",
    "tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n",
    "tools = [tool]\n",
    "\n",
    "# Get the prompt to use - you can modify this!\n",
    "# If you want to see the prompt in full, you can at: https://smith.langchain.com/hub/hwchase17/openai-functions-agent\n",
    "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",
    "\n",
    "llm = ChatOpenAI(temperature=0)\n",
    "\n",
    "agent = create_openai_functions_agent(llm, tools, prompt)\n",
    "agent_executor = AgentExecutor(\n",
    "    agent=agent, tools=tools, verbose=True, return_intermediate_steps=True\n",
    ")\n",
    "response = agent_executor.invoke({\"input\": \"What is Leo DiCaprio's middle name?\"})\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(AgentActionMessageLog(tool='wikipedia', tool_input='Leonardo DiCaprio', log='\\nInvoking: `wikipedia` with `Leonardo DiCaprio`\\n\\n\\n', message_log=[AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '{\"__arg1\":\"Leonardo DiCaprio\"}', 'name': 'wikipedia'}}, response_metadata={'finish_reason': 'function_call'}, id='run-6b7bb264-af90-4f2b-9eaf-0eecf0021621')]), 'Page: Leonardo DiCaprio\\nSummary: Leonardo Wilhelm DiCaprio (; Italian: [diˈkaːprjo]; born November 1')]\n"
     ]
    }
   ],
   "source": [
    "# The actual return type is a NamedTuple for the agent action, and then an observation\n",
    "print(response[\"intermediate_steps\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
